package net.thxjj.adminServer.mongodao;

import net.thxjj.adminServer.entity.LoginHadleEvent;
import net.thxjj.adminServer.entity.aep.LoginData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class MongoLoginDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    //导入所有数据
    public void saveAllData(List<LoginData> list){
        mongoTemplate.insert(list,LoginData.class);
    }


    //导入校验（如果有timestamp和deviceId重合则更新）
    public void saveLoginDatas(List<LoginData> list){

        for (LoginData profile:
                list) {
            LoginData po = findProfileLoginDataByDeviceIdAndTime(profile.getDeviceId(),profile.getTimestamp());
            if(po==null){
                mongoTemplate.save(profile);
            }else{
                updateProfileLoginData(profile);
            }
        }
    }

    public void saveLoginData(LoginData loginData){
        LoginData po = findProfileLoginDataByDeviceIdAndTime(loginData.getDeviceId(),loginData.getTimestamp());
        if(po==null){
            mongoTemplate.save(loginData);
        }else{
            updateProfileLoginData(loginData);
        }
    }

    private void updateProfileLoginData(LoginData loginData) {

        Query query = new Query(Criteria.where("deviceId").is(loginData.getDeviceId()).and("timestamp").is(loginData.getTimestamp()));
        Update update = new Update()
                .set("tau_flag",loginData.getTau_flag())
                .set("tau",loginData.getTau())
                .set("manufacturer",loginData.getManufacturer_name())
                .set("hardware_version",loginData.getHardware_version())
                .set("software_version",loginData.getSoftware_version())
                .set("device_type",loginData.getDevice_type())
                .set("active_time_flag",loginData.getActive_time_flag())
                .set("active_time",loginData.getActive_time())
                .set("IMSI",loginData.getIMSI())
                .set("IMEI",loginData.getIMEI());
        mongoTemplate.upsert(query,update, LoginData.class);
    }


    private LoginData findProfileLoginDataByDeviceIdAndTime(String deviceId,long time){
        Query query = new Query(Criteria.where("deviceId").is(deviceId).and("timestamp").is(time));
        return mongoTemplate.findOne(query,LoginData.class);
    }

    public LoginData getNewestLoginInfo(String deviceId){
        Query query = new Query(Criteria.where("deviceId").is(deviceId));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.findOne(query,LoginData.class);
    }


    public List<LoginData> getAllLoginInfo(String device_id){
        Query query = new Query(Criteria.where("deviceId").is(device_id));
        query.with(new Sort(Sort.Direction.ASC,"timestamp"));
        return mongoTemplate.find(query,LoginData.class);
    }

    public List<LoginData> getLoginInfoByTime(String device_id,long begin_time){
        Query query = new Query(Criteria.where("deviceId").is(device_id).and("timestamp").gte(begin_time));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.find(query,LoginData.class);
    }

    public List<LoginData> getLoginDataAtTime(String device_id, long start_time, long end_time){
        Query query = new Query(Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("timestamp").gt(start_time),Criteria.where("timestamp").lt(end_time)));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        return mongoTemplate.find(query,LoginData.class);
    }

    public List<LoginHadleEvent> getLoginHandleWithLog(String device_id){
        List<LoginData> list=  getAllLoginInfo(device_id);
        ArrayList<LoginHadleEvent> list1 = new ArrayList<>();
        for (LoginData logindata:
                list) {
            LoginHadleEvent loginHadleEvent = new LoginHadleEvent().setDeviceId(logindata.getDeviceId()).setTimestamp(logindata.getTimestamp()).setUserId(0);
            list1.add(loginHadleEvent);
        }
        Query query = new Query(Criteria.where("deviceId").is(device_id));
        query.with(new Sort(Sort.Direction.ASC,"timestamp"));
        List<LoginHadleEvent> list2 = mongoTemplate.find(query,LoginHadleEvent.class);
        list1.addAll(list2);
        Collections.sort(list1);
        return list1;
    }
}
